Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
Javascript arrow function weird behaviour with "this" keyword

This arrow function prints the concatenated string version of the array correctly:

var tahoe = {
  resorts: ["Kirkwood","Squaw","Alpine","Heavenly","Northstar"],
  print: function(delay=1000) {

    setTimeout(
      () => console.log(this.resorts.join(",")),
      delay
    )

  }
}

tahoe.print()

If I understand correctly, if I use a normal function inside the setTimeout, it will not work, because setTimeout requires a nested function that is aways evaluated in the global scope, while the array is defined within the scope of the object. So the arrow function is used which works in the scope of the object.

Then, how come putting the print function as arrow function as well reverses the problem again?

var tahoe = {
  resorts: ["Kirkwood","Squaw","Alpine","Heavenly","Northstar"],
  print: (delay=1000) => {

    setTimeout(() => {
      console.log(this.resorts.join(","))
    }, delay)

  }
}

tahoe.print()

This doesn't work. The book explains it like this:

enter image description here enter image description here

Can somebody explain what this means, and why it works like this? What is the exact difference between the two cases?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda